package com.choosefine.it.basedata.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.basedata.model.Worker;
import com.choosefine.it.basedata.service.WorkerService;
import com.choosefine.it.common.def.CommonConstant;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.utils.DateUtils;

/**
 * 工人相关控制层
 * 
 * @author guwenwei 2016/11/16
 */
@RestController
@RequestMapping("/basedata/workerinfo")
@Api(value="工人详情相关",description="顾文伟")
public class WorkerController extends BaseController{
	@Autowired
	WorkerService workerService;

	/**
	 * 添加工人
	 * 
	 * @param worker
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value = "存储工人详情")
	@ResponseBody
	@ApiResponses({
			@ApiResponse(code = ResultCode.SUCCESS_CREATED, message = "添加成功返回结果"),
			@ApiResponse(code = ResultCode.FAILED, message = "请求参数错误") })
	@RequestMapping(method = RequestMethod.POST)
	public String save(@RequestBody Worker worker) {
		String result = null;
		if (worker != null) {
			worker.setCtime(DateUtils.getNowTime());
			Integer num = workerService.insert(worker);
			if (num >0) {
				result = responseSuccess(num,ResultCode.SUCCESS_CREATED,ResultCode.MSG_CREATE_SUCCESS);
			} else {
				result = responseFail(ResultCode.MSG_ERROR);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}

	/**
	 * 修改工人详情
	 * 
	 * @param worker
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value = "修改工人详情")
	@ResponseBody
	@ApiResponses({
			@ApiResponse(code = ResultCode.SUCCESS_CREATED, message = "添加成功返回结果"),
			@ApiResponse(code = ResultCode.FAILED, message = "请求参数错误") })
	@RequestMapping(method = RequestMethod.PUT)
	public String update(@RequestBody Worker worker) {
		String result = null;
		if (worker != null) {
			worker.setUtime(DateUtils.getNowTime());
			Integer num = workerService.update(worker);
			if (num >0) {
				result = responseSuccess(num,ResultCode.SUCCESS_CREATED,ResultCode.MSG_UPDATE_SUCCESS);
			} else {
				result = responseFail(ResultCode.MSG_ERROR);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}

	/**
	 * 根据工人编号获取工人详情
	 * @param userCode
	 * @return
	 */
	@ApiOperation(value = "根据用户编号取工人详情")
	@ApiImplicitParam(name = "userCode", value = "用户编号", required = true, dataType = "String", paramType = "path")
	@RequestMapping(value = "/UserCode/{userCode}", method = RequestMethod.GET)
	public String getUserByUserCode(@PathVariable String userCode) {
		String result = null;
		if (userCode != null) {
			Worker worker = workerService.findByUserCode(userCode);
			if (worker != null) {
				result = responseSuccess(worker);
			} else {
				result = responseFail(ResultCode.MSG_ERROR);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}

	// @ApiOperation(value = "根据工人ID取工人详情", notes = "")
	// @ApiImplicitParam(name = "id", value = "工人ID", required = true, dataType
	// = "Integer", paramType = "path")
	// @RequestMapping(value = "/{id}", method = RequestMethod.GET)
	// public JsonResult getUserById(@PathVariable Integer id) {
	// JsonResult result = null;
	// if (id > 0) {
	// Worker worker = workerService.findByPrimaryKey(id);
	// if (worker != null) {
	// result = new JsonResult(worker, ResultCode.MSG_SUCCESS,
	// ResultCode.SUCCESS);
	// } else {
	// result = new JsonResult(null, ResultCode.MSG_ERROR,
	// ResultCode.FAILED);
	// }
	// } else {
	// result = getInstanceJsonResult(null, "参数不能为空",
	// ResultCode.SERVERERROR_INTERNAL);
	// }
	// return result;
	// }

	/**
	 * 根据用户编号伪删除工人详情
	 * @param UserCode
	 * @return
	 */
	@ApiOperation(value = "根据用户编号删除工人详情")
	@ApiImplicitParam(name = "userCode", value = "用户编号", required = true, dataType = "String", paramType = "path")
	@RequestMapping(value = "/UserCode/{userCode}", method = RequestMethod.DELETE)
	public String deleteByUserCode(@PathVariable String userCode) {
		String result = null;
		if (userCode != null) {
			Map<String, Object> params = new HashMap<>();
			params.put("isValid", CommonConstant.INVALID);
			params.put("userCode", userCode);
			params.put("utime", DateUtils.getNowTime());
			int num = workerService.deleteByUserCode(params);
			if (num > 0) {
				result = responseSuccess(num);
			} else {
				result = responseFail(ResultCode.MSG_ERROR);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}

}
